

local file_path_in "/tochange/"

timer clear 1
timer on 1

** want to create shock at both the county and zipcode level
local X FIPS_CODE zip
local Y fips zip
local Z County Zip
local R FIPS ZIP

local n : word count `X'
forvalues i=1/`n' {
	local xi : word `i' of `X'
	local yi : word `i' of `Y'
	local zi : word `i' of `Z'
	local ri : word `i' of `R'
	
	display " `xi' `yi' `zi' `ri'"
	
	use "`file_path_in'/builder county/rawBUILDERall_`ri'.dta" , clear 

	** merge in hp indices 
	merge m:1 `xi' date using "`file_path_in'/house price index/allHPchgs_`yi'.dta", 
	keep if _merge==3 	
	drop _merge

	** drop if shock is missing 
	 drop if delta1yrZIL_`yi'==.

	sort seller date

	bysort seller date: gen totalsold_`yi' = sum(nsold_`yi')
	bysort seller date: replace totalsold_`yi' = totalsold_`yi'[_N]
	bysort seller date: gen weight_`yi' = (nsold_`yi'/totalsold_`yi')
		
	local shocklist delta1yrZIL_`yi' delta1moZIL_`yi'

	foreach t of local shocklist {
		
		bysort seller date: gen dep`t' = weight_`yi'*(`t')   
		bysort seller date: gen dep4 = sum(dep`t')
		bysort seller date: replace dep4 = dep4[_N]

		** create shock measure: builder x county x time
		bysort seller date: gen shock`t' = dep4-dep`t'   /* this represents the builder's exposure in all other counties except the county of that row */
		bysort seller date: gen todrop = 1 if mi(shock`t')
		bysort seller date: egen misschg= max(todrop)
		drop misschg todrop 
		drop dep4
	}
	order seller date year `xi' nsold_`yi' totalsold_`yi'  shock* 

	replace shockdelta1yrZIL_`yi' = 0 if seller=="ACTUALPERSON"
	replace shockdelta1moZIL_`yi' = 0 if seller=="ACTUALPERSON"

	if "`xi'"=="zip" {
		rename zipcode zipcode_long
	}	
	saveold "`file_path_in'/builder county/rawBUILDERmergedHPshock_`yi'.dta", version(13) replace
}
	
** merge shock to transactions data 
use "`file_path_in'/house county//rawHOUSEBUILDERall.dta", clear
append using "`file_path_in'/builder county//land_sales_bldrs.dta" 
merge m:1 seller date FIPS_CODE using "`file_path_in'/builder county/rawBUILDERmergedHPshock_fips.dta", keep(master match) nogen
merge m:1 seller date zip using "`file_path_in'/builder county/rawBUILDERmergedHPshock_zip.dta", keep(master match) nogen
merge m:1 seller FIPS_CODE using "`file_path_in'/builder county/rawBUILDERmergedHPshockAGGR_fips.dta", keep(master match) nogen  /* merge in builder-county shock measures */
merge m:1 seller zip using "`file_path_in'/builder county/rawBUILDERmergedHPshockAGGR_zip.dta", keep(master match) nogen  /* merge in builder-zip shock measures */
encode PROPERTY_STATE, gen(state1)
rename state state_ab
rename state1 state
encode PROPERTY_CITY, gen(city1)
rename city city2
rename city1 city 

** get hhi of builder at county level
preserve
	 do "make-builder-concentration.do"
restore
merge m:1 seller year FIPS_CODE using "`file_path_in'/builderhhi_FIPS_CODE.dta" ,nogen

saveold "`file_path_in'/HOUSEBUILDERshockHHI.dta", version(13) replace

sample 1
saveold "`file_path_in'/HOUSEBUILDERshocksampleHHI.dta", version(13) replace


timer off 1
timer list 1
